package cs348a.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import cs348a.data.opt.NearestOptimizer;
import cs348a.data.opt.PathOptimizer;

public class TourData {
	/** List of sites */
	public ArrayList<Site> siteList = new ArrayList<Site>();
	
	public List<Site> optSiteList = null;
	private PathOptimizer po = new NearestOptimizer();
	
	public String delimiter = new String(" ");
	
	// Stats of site data
	public int minX = Integer.MAX_VALUE;
	public int maxX = Integer.MIN_VALUE;

	public int minY = Integer.MAX_VALUE;
	public int maxY = Integer.MIN_VALUE;

	public float minZ = Integer.MAX_VALUE;
	public float maxZ = Integer.MIN_VALUE;
	
	/**
	 * Returns the number of sites.
	 * 
	 * @return
	 */
	public int numSites() {
		return siteList.size();
	}
	
	/**
	 * Optimizes the path
	 */
	public void optimizeTourPath() {
		// get distances between all paths
		optSiteList = po.optimizePath(siteList);
		
		
	}

	/**
	 * Parses the data file to create data structures
	 * 
	 * @param file
	 */
	public void parseDataFile(File file) {		
		try {
			BufferedReader input =  new BufferedReader(new FileReader(file));
			
			try {
				resetData();
				
				String line = null;
				
				int i = 0;
				
				while (( line = input.readLine()) != null){
					Scanner scanner = new Scanner(line);
					scanner.useDelimiter(delimiter);
					
					if(scanner.hasNext()) {
						int x = new Float(scanner.next()).intValue();
						int y = new Float(scanner.next()).intValue();
						float z = new Float(scanner.next());
						
						System.out.println(x+"/"+y+"/"+z);
						
						siteList.add(new Site(x, y, z, i++));
						
						// get stats
						if(x < minX) minX = x;
						if(x > maxX) maxX = x;
						if(y < minY) minY = y;
						if(y > maxY) maxY = y;
						if(z < minZ) minZ = z;
						if(z > maxZ) maxZ = z;
					}
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			finally {
				input.close();
			}
		}
	    catch (IOException e){
	      e.printStackTrace();
	    }
	}
	
	/**
	 * Resets the data
	 */
	private void resetData() {
		siteList.clear();
		
		minX = Integer.MAX_VALUE;
		maxX = Integer.MIN_VALUE;

		minY = Integer.MAX_VALUE;
		maxY = Integer.MIN_VALUE;

		minZ = Integer.MAX_VALUE;
		maxZ = Integer.MIN_VALUE;
	}
}
